package net.tv90.auth.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.tv90.auth.model.domain.Account;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface AccountMapper extends BaseMapper<Account> {

    @Select("select count(*) from auth.account where username = #{username}")
    Integer checkUserExistByUserName(String username);

    @Select("select count(*) from auth.account where email = #{email}")
    Integer checkUserExistByEmail(String email);


    default Account tryLoginUserByUsername(String username, String password) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Account account = selectOne(queryWrapper);
        if (account != null && account.getPassword().equals(password)) {
            return account;
        }
        return null;
    }

    default Account tryLoginUserByEmail(String email, String password) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        Account account = selectOne(queryWrapper);
        if (account != null && account.getPassword().equals(password)) {
            return account;
        }
        return null;
    }

    default void doLogin(Account account) {
        updateById(account);
    }

}
